import {createSlice, PayloadAction} from "@reduxjs/toolkit";
import {RootState} from "../store";

export interface WorkInfo {
    beginDate: string,
    endDate: string,
    companyName: string,
    jobTitle: string,
    industry: string,
    detail: string
}
export type WorKInfoList = WorkInfo[]
export type WorkInfoKey = keyof WorkInfo

export const workInitialState: WorKInfoList = [
    {
        beginDate: '',
        endDate: '',
        companyName: '',
        jobTitle: '',
        industry: '',
        detail: ''
    }
]

export const workInfoSlice = createSlice({
    initialState: workInitialState,
    name: 'workInfo',
    reducers: {
        addWorkInfoItem: (state) => {
            state.push({
                beginDate: '', endDate: '',
                companyName: '', jobTitle: '',
                industry: '', detail: ''
            })
        },
        updateWorkInfo: (state, action: PayloadAction<{index: number, updateValue: {[p in WorkInfoKey]?: string}} >) => {
            const { index, updateValue } = action.payload
            state[index] = {...state[index], ...updateValue}
        },
        deleteWorkInfo: (state, action: PayloadAction<{index: number}>) => {
            const { index } = action.payload
            state.splice(index, 1)
        },
        reInitWorkInfo: (state, action) => action.payload,
    }
})
export const {
    addWorkInfoItem, updateWorkInfo, deleteWorkInfo, reInitWorkInfo
} = workInfoSlice.actions;

export const getWorkInfo = (state: RootState) => state.workInfo

export default workInfoSlice.reducer;
